﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core;
using ZHDJ.Data.Models.Common;
using ZHDJ.PersonalCenter.BLL;
using ZHDJ.WebPage;

namespace ZHDJ.PersonalCenter.Module.Task
{
    public partial class PartyTaskList : PageBase
    {
        /// <summary>
        /// 待办状态 1 待办 2已办 3 取消  4 删除
        /// </summary>
        public SysEnum.TaskType TaskType
        {
            get
            {
                try
                {
                    var id = GetQueryIntValue("TaskType");
                    SysEnum.TaskType type = (SysEnum.TaskType)Enum.Parse(typeof(SysEnum.TaskType), id);
                    return type;
                }
                catch (Exception)
                {
                    return SysEnum.TaskType.ToDo;
                }

            }
        }

        public string toUser
        {
            get
            {
                var temp = GetQueryIntValue("TaskType");
                return string.IsNullOrEmpty(temp) ? "1" : temp;
            }
        }
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "";
            }
        }

        #endregion



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        #region 数据加载
        private void BindData()
        {
            ControlBusiness.ControlBindDataDictionary(
               ddlCategory, SysDataDictionary.TaskCategory, true);
            PageContext.RegisterStartupScript(Window1.GetShowReference() + Window1.GetMaximizeReference() + Window1.GetHideReference());
            // 每页记录数
            Grid1.PageSize = ConfigHelper.PageSize;
            ddlGridPageSize.SelectedValue = ConfigHelper.PageSize.ToString();
            if ((int)TaskType != 1)
            {
                Grid1.Columns[7].HeaderText = "查看";
            }
        }

        private void LoadData()
        {
            var ds = PartyTask.GetGridData(Grid1.PageIndex, Grid1.PageSize, ddlCategory.SelectedValue, txtTitle.Text, TaskType, SysEnum.TaskSpecie.Member, CurrentUser.LoginID);
            Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            Grid1.DataSource = ds.Tables[0];
            Grid1.DataBind();
        }

        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            //CheckPowerWithWindowField("PartyTaskEdit", Grid1, "editField");
            //CheckPowerWithLinkButtonField("PartyTaskDelete", Grid1, "deleteField");
        }


        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            LoadData();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string PartyTaskId = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
               
                var del = DB.PartyTasks.FirstOrDefault(t => t.ID == PartyTaskId);
                del.TaskType = "4";
                DB.SaveChanges();
                LoadData();
            }
        }


        #endregion

        #region 按钮事件
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {


            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            DB.PartyTasks.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => u.TaskType = "4");
            DB.SaveChanges();
            //DB.Users.Delete(u => ids.Contains(u.ID));


            // 重新绑定表格
            LoadData();
        }
        protected void Grid1_PreRowDataBound(object sender, GridPreRowEventArgs e)
        {
            DataRowView row = e.DataItem as DataRowView;
            var btnHandler = Grid1.FindColumn("btnHandler") as LinkButtonField;

            if (btnHandler != null)
            {
                this.Window1.Title = Convert.ToString(row["Title"]);
                var url = Convert.ToString(row["TaskUrl"]);
                if (!url.Contains("TaskID="))
                {
                    if (url.Contains("?"))
                    {
                        url += "&TaskID=" + row["ID"];
                    }
                    else
                    {
                        url += "?TaskID=" + row["ID"];
                    }
                }
                url += "&TaskType=" + GetQueryIntValue("TaskType");
                btnHandler.OnClientClick = this.Window1.GetShowReference(url, Convert.ToString(row["Title"]));
            }

        }



        #endregion

        protected void Window1_Close(object sender, EventArgs e)
        {
            LoadData();
        }


        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            LoadData();
        }
    }
}
